﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Student
{
    class Information
    {

        // Declare List of Student
        GeniusList<Student> List;

        // Declare handling Data Grid View
        public DataGridView Grid;

        // Function : Setting the List & Data Grid View
        // Build : 1000 - Release Date : Oct 17th , 2013
        public Information(DataGridView tmpGrid)
        {

            // Setup the List
            this.List = new GeniusList<Student>();

            // Setup the Data Grid View
            this.Grid = tmpGrid;

            // Update the Data Grid View
            Update();

        }

        // Function : Load the Student List from a Text File
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Load()
        {

            // Ask for Appending
            if (MessageBox.Show("Append to Current List ?", "Student Management", MessageBoxButtons.YesNo) == DialogResult.No)
            {

                // Clear the List
                this.List = new GeniusList<Student>();

            }

            // Create new File Open Dialog
            OpenFileDialog openDialog = new OpenFileDialog();

            // Set the Title
            openDialog.Title = "Choose Student List File";

            // Check the Open Status
            if (openDialog.ShowDialog() == DialogResult.OK)
            {

                // Read all of Lines in the File
                String[] tmpLines = System.IO.File.ReadAllLines(openDialog.FileName);

                // Walk around Lines
                for (int X = 0; X < tmpLines.Length; X++)
                {

                    // Validate the Line
                    if (tmpLines[X].Trim().Length > 4)
                    {

                        // Split the Line
                        String[] tmpRow = tmpLines[X].Trim().Split('|');

                        // Validate the Row
                        if (tmpRow.Length == 4)
                        {

                            // Generate a new Student object
                            Student tmpStudent = new Student() { Code = tmpRow[0].Trim(), Name = tmpRow[1].Trim(), Birthday = tmpRow[2].Trim(), Birthplace = tmpRow[3].Trim() };

                            // Append this Student into the List
                            Append(tmpStudent);

                        }

                    }

                }

            }

        }

        // Function : Append new Student into List
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Append(Student tmpStudent)
        {

            // Get the Student Code
            String tmpCode = tmpStudent.Code;

            // Walk around the List
            for (int X = 0; X < List.Count(); X++)
            {

                // Compare the Code
                if (tmpCode.Equals(List.Element(X).Code))
                {

                    // Show the Error
                    MessageBox.Show("The Student Code existed !", "Student Management");

                    // Break the Operation
                    return;

                }

            }

            // Insert into List
            this.List.Append(tmpStudent);

            // Update the Data Grid View
            Update();

        }
        
        // Function : Delete a Student from the List
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Delete(String tmpCode)
        {

            // Check the Code
            if (tmpCode.Length != 7)
            {

                // Break the Operation
                return;

            }

            // Walk around the List
            for (int X = 0; X < List.Count(); X++)
            {

                // Compare the Code
                if (List.Element(X).Code.Equals(tmpCode))
                {

                    // Delete the Student
                    List.Remove(X);

                    // Break the Operation
                    break;

                }

            }

            // Update the List
            Update();

        }

        // Function : Reorder the Student List
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Reorder(int tmpKind)
        {

            // Check the Kind
            if (tmpKind == 0)
            {

                // Walkthrought the List
                for (int X = 0; X < this.List.Count(); X++)
                {

                    // Get the Root Student Code
                    String tmpRootCode = this.List.Element(X).Code;
                    int tmpRootCodeChar = (int)char.Parse(tmpRootCode.Substring(0, 1));
                    int tmpRootCodeNumber = int.Parse(tmpRootCode.Substring(1, 6));
                    int tmpRoot = tmpRootCodeChar + tmpRootCodeNumber;

                    // Set the Minimum Value
                    int tmpMinimum = tmpRoot;

                    // Set the Minimum Position
                    int tmpMinimumPosition = X;

                    // Walkthrought the rest of the List
                    for (int Y = X; Y < this.List.Count(); Y++)
                    {

                        // Find the Minimum
                        for (int Z = Y; Z < this.List.Count(); Z++)
                        {

                            // Get the Current Student Code Number
                            String tmpCurrentCode = this.List.Element(Z).Code;
                            int tmpCurrentCodeChar = (int)char.Parse(tmpCurrentCode.Substring(0, 1));
                            int tmpCurrentCodeNumber = int.Parse(tmpCurrentCode.Substring(1, 6));
                            int tmpCurrent = tmpCurrentCodeChar + tmpCurrentCodeNumber;

                            // Compare with the Minimum
                            if (tmpCurrent < tmpMinimum)
                            {

                                // Update the Minimum
                                tmpMinimum = tmpCurrent;

                                // Update the Minimum Position
                                tmpMinimumPosition = Z;

                            }

                        }

                    }

                    // Swap the Minimum Position
                    this.List.Swap(X, tmpMinimumPosition);

                }

            }
            else
            {

                // Walkthrought the Student List
                for (int X = 0; X < this.List.Count(); X++)
                {

                    // Get the Root Student Name
                    String[] tmpRootFullName = this.List.Element(X).Name.Split(' ');
                    String tmpRootName = tmpRootFullName[tmpRootFullName.Count() - 1].ToUpper();
                    int tmpRoot = (int) VietnameseConvert(tmpRootName[0]);

                    // Set the Minimum Value
                    int tmpMinimum = tmpRoot;

                    // Set the Minimum Position
                    int tmpMinimumPosition = X;

                    // Walkthrought the rest of the List
                    for (int Y = X; Y < this.List.Count(); Y++)
                    {

                        // Find the Minimum
                        for (int Z = Y; Z < this.List.Count(); Z++)
                        {

                            // Get the Current Student Name
                            String[] tmpCurrentFullName = this.List.Element(Z).Name.Split(' ');
                            String tmpCurrentName = tmpCurrentFullName[tmpCurrentFullName.Count() - 1].ToUpper();
                            int tmpCurrent = (int)VietnameseConvert(tmpCurrentName[0]);

                            // Compare with the Minimum
                            if (tmpCurrent < tmpMinimum)
                            {

                                // Update the Minimum
                                tmpMinimum = tmpCurrent;

                                // Update the Minimum Position
                                tmpMinimumPosition = Z;

                            }

                        }

                    }

                    // Swap the Minimum Position
                    this.List.Swap(X, tmpMinimumPosition);

                }

            }

            // Update the Data Grid View
            Update();

        }
        
        // Function : Save the List into Text File
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Save()
        {

            // Validate the Number of Element
            if (this.List.Count() < 1)
            {

                // Show the Error
                MessageBox.Show("The List is empty !", "Student Management");

                // Break the Operation
                return;

            }

            // Create a new Array
            String[] tmpLines = new String[this.List.Count()];

            // Walk around the List
            for (int X = 0; X < this.List.Count(); X++)
            {

                // Get the Information of each Student
                String tmpCode = this.List.Element(X).Code;
                String tmpName = this.List.Element(X).Name;
                String tmpBirthday = this.List.Element(X).Birthday;
                String tmpBirthplace = this.List.Element(X).Birthplace;

                // Generate a String of Student Information
                String tmpInformation = tmpCode + "|" + tmpName + "|" + tmpBirthday + "|" + tmpBirthplace;

                // Insert the String into Array
                tmpLines[X] = tmpInformation;

            }

            // Generate a Save File Dialog
            SaveFileDialog saveDialog = new SaveFileDialog();

            // Change the Dialog Title
            saveDialog.Title = "Choose Text File";

            // Change the Default Extension
            saveDialog.DefaultExt = "TXT";

            // Change the Filter
            saveDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            // Show and Wirte File
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {

                // Save the List of Student into File
                System.IO.File.WriteAllLines(saveDialog.FileName, tmpLines);

            }

        }

        // Function : Update the Data Grid View
        // Build : 1000 - Release Date : Oct 17th , 2013
        public void Update()
        {

            // Clear the Data Grid View
            this.Grid.DataSource = null;

            // Generate the List of Element
            List<Student> tmpList = this.List.ToList();

            // Declare the Data Binding
            BindingSource tmpBinding = new BindingSource();

            // Add the List as Data Source into Data Biding
            tmpBinding.DataSource =  tmpList;

            // Insert Data Source into Grid View
            this.Grid.DataSource = tmpBinding;

            // Decorate the Data Grid View
            this.Grid.Columns[0].Width = 60;
            this.Grid.Columns[1].Width = 160;
            this.Grid.Columns[2].Width = 220;

            // Remove the List
            tmpList = null;

        }

        // Function : Change the Vietnamese to Non-Vietnamese
        // Build : 1000 - Release Date : Oct 17th , 2013
        private Char VietnameseConvert(Char tmpChar)
        {

            // Switch the tmpChar
            if (tmpChar == 'Á') { return 'A'; }
            if (tmpChar == 'À') { return 'A'; }
            if (tmpChar == 'Ả') { return 'A'; }
            if (tmpChar == 'Ã') { return 'A'; }
            if (tmpChar == 'Ạ') { return 'A'; }

            if (tmpChar == 'Â') { return 'A'; }
            if (tmpChar == 'Ấ') { return 'A'; }
            if (tmpChar == 'Ầ') { return 'A'; }
            if (tmpChar == 'Ẩ') { return 'A'; }
            if (tmpChar == 'Ẫ') { return 'A'; }
            if (tmpChar == 'Ậ') { return 'A'; }

            if (tmpChar == 'Ă') { return 'A'; }
            if (tmpChar == 'Ắ') { return 'A'; }
            if (tmpChar == 'Ằ') { return 'A'; }
            if (tmpChar == 'Ẳ') { return 'A'; }
            if (tmpChar == 'Ẵ') { return 'A'; }
            if (tmpChar == 'Ặ') { return 'A'; }

            if (tmpChar == 'Đ') { return 'D'; }

            if (tmpChar == 'É') { return 'E'; }
            if (tmpChar == 'È') { return 'E'; }
            if (tmpChar == 'Ẻ') { return 'E'; }
            if (tmpChar == 'Ẽ') { return 'E'; }
            if (tmpChar == 'Ẹ') { return 'E'; }

            if (tmpChar == 'Ê') { return 'E'; }
            if (tmpChar == 'Ế') { return 'E'; }
            if (tmpChar == 'Ề') { return 'E'; }
            if (tmpChar == 'Ể') { return 'E'; }
            if (tmpChar == 'Ễ') { return 'E'; }
            if (tmpChar == 'Ệ') { return 'E'; }

            if (tmpChar == 'Í') { return 'I'; }
            if (tmpChar == 'Ì') { return 'I'; }
            if (tmpChar == 'Ỉ') { return 'I'; }
            if (tmpChar == 'Ĩ') { return 'I'; }
            if (tmpChar == 'Ị') { return 'I'; }

            if (tmpChar == 'Ó') { return 'O'; }
            if (tmpChar == 'Ò') { return 'O'; }
            if (tmpChar == 'Ỏ') { return 'O'; }
            if (tmpChar == 'Õ') { return 'O'; }
            if (tmpChar == 'Ọ') { return 'O'; }

            if (tmpChar == 'Ô') { return 'O'; }
            if (tmpChar == 'Ố') { return 'O'; }
            if (tmpChar == 'Ồ') { return 'O'; }
            if (tmpChar == 'Ổ') { return 'O'; }
            if (tmpChar == 'Ỗ') { return 'O'; }
            if (tmpChar == 'Ộ') { return 'O'; }

            if (tmpChar == 'Ơ') { return 'O'; }
            if (tmpChar == 'Ớ') { return 'O'; }
            if (tmpChar == 'Ờ') { return 'O'; }
            if (tmpChar == 'Ở') { return 'O'; }
            if (tmpChar == 'Ỡ') { return 'O'; }
            if (tmpChar == 'Ợ') { return 'O'; }

            if (tmpChar == 'Ú') { return 'U'; }
            if (tmpChar == 'Ù') { return 'U'; }
            if (tmpChar == 'Ủ') { return 'U'; }
            if (tmpChar == 'Ũ') { return 'U'; }
            if (tmpChar == 'Ụ') { return 'U'; }

            if (tmpChar == 'Ư') { return 'U'; }
            if (tmpChar == 'Ứ') { return 'U'; }
            if (tmpChar == 'Ừ') { return 'U'; }
            if (tmpChar == 'Ử') { return 'U'; }
            if (tmpChar == 'Ữ') { return 'U'; }
            if (tmpChar == 'Ự') { return 'U'; }

            if (tmpChar == 'Ý') { return 'U'; }
            if (tmpChar == 'Ỳ') { return 'U'; }
            if (tmpChar == 'Ỷ') { return 'U'; }
            if (tmpChar == 'Ỹ') { return 'U'; }
            if (tmpChar == 'Ỵ') { return 'U'; }


            // Return tmpChar
            return tmpChar;

        }

    }
}
